      SUBROUTINE TOCART(U,KEPLER,IANOM,POS,VEL,IERPNT,IERR)
C
C     PURPOSE: TOCART CONVERTS THE KEPLERIAN ELEMENTS OF A VEHICLE
C              IN ORBIT INTO CARTESIAN COORDINATES
C
C
C
C  VAR     DIM  TYPE   I/O   DESCRIPTION
C  ---     ---  ----   ---   -----------
C
C  U       1    R*8     I    CENTRAL BODY GRAVITATION CONSTANT.
C                            EX: U=398601.2 KM**3/SEC**2
C                            LENGTH UNIT IS THE SAME AS SEMI-MAJOR
C                            AXIS UNIT. TIME UNIT IS THE SAME AS
C                            FOR VELOCITY TO BE OUTPUT.
C
C  KEPLER  6    R*8     I    KEPLERIAN ELEMENTS TO BE CONVERTED TO
C                            CARTESIAN COORDINATES. LENGTH UNITS ARE
C                            THE SAME AS FOR U. ANGLE UNITS ARE
C                            RADIANS. ORDER IS: SMA, ECC, INCL, NODE,
C                            ARGP, ANOM(MEAN OR TRUE: SEE IANOM).
C
C  IANOM   1    I*4     I    ANOMALY FLAG.
C                            =0 MEANS KEPLER(6) IS MEAN ANOMALY.
C                            =NON-ZERO MEANS KEPLER(6) IS TRUE ANOMALY.
C
C  POS     3    R*8     O    CARTESIAN POSITION VECTOR. LENGTH UNIT IS
C                            AS FOR U AND ELEM(1).
C
C  VEL     3    R*8     O    CARTESIAN VELOCITY VECTOR. LENGTH UNIT IS
C                            AS FOR R. TIME UNIT IS AS FOR U.
C
C  IERPNT  1    I*4     I    FORTRAN UNIT FOR ERROR MESSAGE. NONE
C                            POSSIBLE IF =0. ERROR MESSAGE IS FOR A 
C                            NON-ELLIPSE/CIRCLE INPUT.
C
C  IERR    1    I*4     O    ERROR RETURN FLAG.
C                            =0 NO ERROR.  =1, ERROR.
C
C*******************************************************************
C
C  CODING: TAKEN FROM 580'S MAESTRO PROGRAM. 
C          C PETRUZZO 9/81.
C          MODIFICATIONS:
C
C*******************************************************************
C
      IMPLICIT REAL*8 (A-H,O-Z)
      REAL*8 INCL,NODE,MEAN,KEPLER(6),P(3,2),RTEMP(2),VTEMP(2),MU
      REAL*8 POS(3),VEL(3)
      REAL*8 PI/  3.141592653589793D0    /
      REAL*8 TWOPI/  6.283185307179586D0    /
C
C
C
      MU=U
      SMA=KEPLER(1)
      ECC=KEPLER(2)
      INCL=KEPLER(3)
      NODE=KEPLER(4)
      ARGP=KEPLER(5)
C
C     ERROR CHECKS.
      IERR=0
      IF(ECC.LT.0.D0 .OR. ECC.GE.1.D0) IERR=1
      IF(SMA.LE.0.D0) IERR=1
      IF(IERR.NE.0) GO TO 1400
C
C     WE NEED TRUE ANOMALY FOR CALCULATIONS. BE SURE WE HAVE IT.
      IF(IANOM.EQ.0) GO TO 10
C     KEPLER(6) IS TRUE ANOMALY.
      TRANOM=DMOD(DABS(KEPLER(6)),TWOPI)
      IF(KEPLER(6).LT.0.D0) TRANOM=TWOPI-TRANOM
      GO TO 20
   10 CONTINUE
C     KEPLER(6) IS MEAN ANOMALY.
      MEAN=DMOD(DABS(KEPLER(6)),TWOPI)
      IF(KEPLER(6).LT.0.D0) MEAN=TWOPI-MEAN
      TRANOM=ANOMLY(+1,MEAN,ECC,0,IDUM)
C
   20 CONTINUE
C     BE SURE CONVENTIONS ARE SATISFIED.
      IF(INCL.EQ.0.D0) NODE=0.D0
      IF(ECC.EQ.0.D0) ARGP=0.D0
C
C
C  FORMULATION FROM DOCUMENT X-582-76-77(APRIL 1976), PAGE 3-49.
C  MATH THEORY OF THE G.T.D.S.
C
      COSTRU=DCOS(TRANOM)
      ECANOM=DARCOS(  (COSTRU+ECC)/(1.D0+ECC*COSTRU)  )
      IF(TRANOM.GT.PI) ECANOM=TWOPI-ECANOM
      SINEC=DSIN(ECANOM)
      COSEC=DCOS(ECANOM)
      DUM1=DSQRT(1.D0-ECC*ECC)
      RTEMP(1) = SMA * (COSEC-ECC)
      RTEMP(2) = SMA * DUM1 * SINEC
      DUM2=DSQRT(MU/SMA)/(1.D0-ECC*COSEC)
      VTEMP(1)=-DUM2*SINEC
      VTEMP(2)=DUM2*DUM1*COSEC
C
      COSAP=DCOS(ARGP)
      SINAP=DSIN(ARGP)
      COSNOD=DCOS(NODE)
      SINNOD=DSIN(NODE)
      COSINC=DCOS(INCL)
      SININC=DSIN(INCL)
      P(1,1)= COSAP*COSNOD - SINAP*SINNOD*COSINC
      P(2,1)= COSAP*SINNOD + SINAP*COSNOD*COSINC
      P(3,1)= SINAP*SININC
      P(1,2)=-SINAP*COSNOD - COSAP*SINNOD*COSINC
      P(2,2)=-SINAP*SINNOD + COSAP*COSNOD*COSINC
      P(3,2)= COSAP*SININC
C
      POS(1) = P(1,1)*RTEMP(1) + P(1,2)*RTEMP(2)
      POS(2) = P(2,1)*RTEMP(1) + P(2,2)*RTEMP(2)
      POS(3) = P(3,1)*RTEMP(1) + P(3,2)*RTEMP(2)
      VEL(1) = P(1,1)*VTEMP(1) + P(1,2)*VTEMP(2)
      VEL(2) = P(2,1)*VTEMP(1) + P(2,2)*VTEMP(2)
      VEL(3) = P(3,1)*VTEMP(1) + P(3,2)*VTEMP(2)
      GO TO 9000
C
 1400 CONTINUE
      IF(IERPNT.NE.0) WRITE(IERPNT,1200) SMA,ECC
 1200 FORMAT('0TOCART. BIG ERROR. ELLIPSE NOT INPUT. ARBITRARY ',
     1  'R/V OUTPUT. IGNORE RESULTS. SMA,ECC IN=',G12.5,1X,G23.16)
      DO 1401 I=1,3
      POS(I)=10000.D0
 1401 VEL(I)=1.D0
C
 9000 CONTINUE
      RETURN
      END
